package com.company.dao;

import com.company.config.DbConfig;
import com.company.model.Class;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDao {
    public List<Class> getClassList() {
        Connection conn=null;
        PreparedStatement stat=null;
        ResultSet rs = null;
        List<Class> classes=new ArrayList<>();

        try{
            conn= DbConfig.getConn();
            stat = conn.prepareStatement("select * from class");
            rs=stat.executeQuery();

            while(rs.next()){
                Class cla=new Class();
                int id=rs.getInt("id");
                String name = rs.getString("name");
                String info =rs.getString("info");
                cla.setId(id);
                cla.setName(name);
                cla.setInfo(info);

                classes.add(cla);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DbConfig.close(conn,stat,rs);
        }

        return classes;
    }

    public boolean addClass(Class scl) {
        Connection conn=null;
        PreparedStatement prst=null;
        ResultSet rs = null;
        try{
            conn=DbConfig.getConn();
            String sql = "insert into class values(null,?,?)";
            prst = conn.prepareStatement(sql);
            prst.setString(1, scl.getName());
            prst.setString(2, scl.getInfo());
            if(prst.executeUpdate() > 0)
                return true;

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DbConfig.close(conn,prst,rs);
        }

        return false;
    }

    public boolean update(Class sc) {
        Connection conn=null;
        PreparedStatement prst=null;
        ResultSet rs = null;
        try{
            conn=DbConfig.getConn();
            String sql = "update class set name=?, info=? where id=?";
            prst = conn.prepareStatement(sql);
            prst.setString(1, sc.getName());
            prst.setString(2, sc.getInfo());
            prst.setInt(3, sc.getId());
            if(prst.executeUpdate() > 0)
                return true;

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DbConfig.close(conn,prst,rs);
        }

        return false;
    }

    public boolean delete(int id) {
        Connection conn=null;
        PreparedStatement prst=null;
        ResultSet rs = null;
        try{
            conn=DbConfig.getConn();
            String sql = "delete from class where id=?";
            prst = conn.prepareStatement(sql);
            prst.setInt(1, id);
            if(prst.executeUpdate() > 0)
                return true;

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DbConfig.close(conn,prst,rs);
        }

        return false;
    }
}
